<template>
  <div>
    <div v-if="!roleShowSta">
      <el-row style="margin-bottom:10px">
        <el-col :span="8" >
          <h4 style="margin:0"> 
            <span>角色管理</span>
            <el-button  type="success" class="addBtn" @click="addFun" v-if="userRole.RL_1601001"><i class="el-icon-plus"></i>角色</el-button>
          </h4>
        </el-col>
      </el-row>
      
      <el-table :data="tableData" border style="width: 100%" >
        <el-table-column prop="name" label="序号" width="60">
          <template slot-scope="scope">{{scope.$index+1}}</template>
        </el-table-column>
        
        <el-table-column prop="created_at" label="创建时间">
           <template slot-scope="scope">{{ timeFun(scope.row.created_at)}}</template>
        </el-table-column>      

        <el-table-column prop="name" label="角色"> </el-table-column>      
        
        <el-table-column label="操作">
          <template slot-scope="scope" >
            <div>
              <!-- <el-button class="addBtn" @click="editFun(scope.row)">设置权限</el-button> -->
              <el-button class="addBtn" @click="roleFun(scope.row)" v-if="userRole.PM_1501001">设置权限</el-button>

              <el-button type="danger"  class="addBtn" @click="deletData(scope.row.id)" v-if="userRole.RL_1601002"> 删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog :title="title" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
        <el-form :model="ruleForm2" :rules="rules2" ref="ruleForm2"  class="demo-ruleForm">
          
          <el-form-item label="" prop="keyword">
            <el-input v-model.number="ruleForm2.keyword"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button  @click="handleClose" style="margin-right:80px">取 消</el-button>
          <el-button type="primary" @click="submitForm('ruleForm2')">确 定</el-button>
        </span>
      </el-dialog>
     
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage4"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </div>
    <Roleedit v-if="roleShowSta"  ref="roleEdit"  :editId="editId" :defualtData="defualtData" :roleData = "roleData" @showFun ="closeVisible"></Roleedit>
   
  </div>
</template>

<script>
  import Http from '@/Http/Http'
  import getTime from '@/Http/getTime'
  // import level from '@/Http/role'
  import Roleedit from './content/Roleedit'

  export default {
    components:{
      Roleedit
    },
    data() {
      var checkAge = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('请输入关键字'));
        } else {
          callback();
        }
      };
      return {
        userRole:JSON.parse(sessionStorage.getItem('userSolt')),
        checkList:[],
        currentPage4: 1,//分野  
        total:0,
        roleShowSta:false,  
        roleData:'',  
        defualtData:'',  
        dialogVisible:false,
        tableData: [],
        page:1,
        editId:'',
        title:'添加角色',
        pageSize:10,
        ruleForm2: {
          keyword: ''
        },
        rules2: {
          keyword: [
            { validator: checkAge, trigger: 'blur' }
          ]
        }
      }
    },
  
    created() {
      this.getlist();
    },
    methods: {
       getlist() {
        
        const parm ={
          "pageIndex":this.page,
          "pageSize":this.pageSize,
        };
        Http.getPermissionlist().then( res => {
          this.defualtData = res.data.data;
        })
        Http.getRolelist(parm)
        .then( res => {
          this.tableData = res.data.data.rows;
          this.total = res.data.data.total;
           this.dialogVisible = false;
          
        })
        .catch( error => {
          
        })
      },  
      addFun(){
        this.dialogVisible = true;
        this.title = '添加角色';
      },
      editFun(val) {
        this.title = '修改角色';
       
        this.editId = val.id;
        this.ruleForm2.keyword = val.keyword;
        this.dialogVisible = true;
      },
      roleFun(row){
        this.roleData = row.permissions;
         this.roleShowSta = true;
         this.editId = row.id;
      },
      closeVisible() {
        this.roleShowSta = false;
        this.editId ='';
        this.getlist()
      },
      timeFun(val) {
        return getTime(val)
      },
      deletData(row) {
        
        this.$confirm(`确认删除此角色吗？`)
        .then(_ => {
          Http.deleteRole(row)
          .then( res => {
            
            if( res.data.code == 200 ){
              this.$message.success('删除成功')
              this.getlist()         
            } else if(res.data.code == -1003) {
              this.$message.error(`请求失败原因：${res.data.message}`)
              this.$router.push('/')
            }
          })
          .catch( error => {
            this.$message.error('服务器请求失败')
          })
        })
        .catch(_ => {
          //取消部分
          
        });
      },
      
      handleClose(done) {
        this.dialogVisible = false
       this.$refs['ruleForm2'].resetFields();
      },
      
       //分页
      handleSizeChange(val) {
        this.pageSize = val;
        this.getlist()
        
      },
      handleCurrentChange(val) {
        this.page = val;
        this.getlist()
        
      },
     
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.editId ? this.editKeywordFun():this.addKeywordFun();
          } else {
            return false;
          }
        });
      },
      addKeywordFun(){
       
        Http.addRole({"name":this.ruleForm2.keyword})
        .then( res => {
          
          if( res.data.code == 200 ) { 
            this.$message.success('角色添加成功')
            this.getlist()    
          } else if (res.data.code == -1003) {
            this.$message.error(`请求失败原因：${res.data.message}`)
            setTimeout(()=>{
              this.$router.push('/')
            },2000)
          } else {
            this.$message.error('关键字添加失败')              
          }
        })
        .catch( error => {
          this.$message.error('服务器请求失败')
          
        })
      },
      editKeywordFun(){
        Http.editKeyword({"keyword":this.ruleForm2.keyword,'id':this.editId})
        .then( res => {
          
          if( res.data.code == 200 ) { 
            this.$message.success('关键字修改成功')
            this.getlist()    
          } else if (res.data.code == -1003) {
            this.$message.error(`请求失败原因：${res.data.message}`)
            setTimeout(()=>{
              this.$router.push('/')
            },2000)
          } else {
            this.$message.error('关键字修改失败')              
          }
        })
        .catch( error => {
          this.$message.error('服务器请求失败')
          
        })
      },
      
    }
  }
</script>